package redisUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Jedis连接池工具类
 */
public class RedisUtils {

    private static JedisPool jedisPool;

    static {
        try {
            //通过类加载器加载配置文件
            InputStream in = RedisUtils.class.getClassLoader().getResourceAsStream("redis.properties");
            Properties properties = new Properties();
            properties.load(in);
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
            config.setMinIdle(Integer.parseInt(properties.getProperty("minIdle")));
            jedisPool = new JedisPool(config,"192.168.10.130",6379);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //定义获取jedis连接对象的方法
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}
